﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Windows.Forms;

namespace 音乐播放器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<string> PList = new List<string>();

        private void button_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择音乐文件";
            ofd.InitialDirectory = @"E:\MY_VS_TEST\VSCTEST\MY_WinformLearn\DO_YOU_LOVE_ME\音乐播放器\Source_wav";
            ofd.Filter = "无损音质|*.wav|低音乐|*.mp3|文本文件|*.txt|所有文件|*.*";
            ofd.Multiselect = true;
            ofd.ShowDialog();
            
            //获取对话框中所有选定文件的文件名
            string []path = ofd.FileNames;
            for (int i = 0; i < path.Length; i++)
            {
                //将音乐文件的文件名加载到listbox
                listBox1.Items.Add(Path.GetFileName(path[i]));
                PList.Add(path[i]);
                //listBox1.SelectedIndex = i;
            }
        }

        /// <summary>
        /// 实现双击播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        SoundPlayer sp = new SoundPlayer();
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {            
            sp.SoundLocation = PList[listBox1.SelectedIndex];
            sp.Play();
        }

        private void button_BackOne_Click(object sender, EventArgs e)
        {
            //if (listBox1.SelectedIndex == listBox1.Items.Count - 1)
            //{
            //    listBox1.SelectedIndex = 0;
            //}
            //else
            //{
            //    listBox1.SelectedIndex++;
            //}

            // 获取当前选中项索引
            int index = listBox1.SelectedIndex;
            index++;
            //如果本次索引达到了上限
            if (index == listBox1.Items.Count)
            {
                index = 0;
            }
            listBox1.SelectedIndex = index;
            sp.SoundLocation = PList[listBox1.SelectedIndex];
            sp.Play();
        }

        private void button_ProOne_Click(object sender, EventArgs e)
        {

            //if (listBox1.SelectedIndex == 0)
            //{
            //    listBox1.SelectedIndex = listBox1.Items.Count - 1;
            //}
            //else
            //{
            //    listBox1.SelectedIndex--;
            //}

            // 获取当前选中项索引
            int index = listBox1.SelectedIndex;
            index--;
            //如果本次索引超过了下限
            if (index < 0 )
            {
                index = listBox1.Items.Count-1;
            }
            listBox1.SelectedIndex = index;
            sp.SoundLocation = PList[listBox1.SelectedIndex];
            sp.Play();
        }

    }
}
